package com.leyou.user.web;

import com.leyou.user.dto.UserDTO;
import com.leyou.user.entity.User;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/info/exists/{data}/{type}")
    public ResponseEntity<Boolean> checkDate(
            @PathVariable("data")String data,
            @PathVariable("type")Integer type){
        return ResponseEntity.ok(userService.checkDate(data, type));
    }

    @PostMapping("/info/code")
    public ResponseEntity<Void> sendCode(@RequestParam("phone")String phone) {
        userService.sendCode(phone);
        return ResponseEntity.ok().build();
    }
    @PostMapping("/info")
    public ResponseEntity<Void> register(@Valid User user, @RequestParam("code") String code){
        userService.register(user, code);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据手机号和密码查询用户
     * @param username
     * @param password
     * @return
     */
    @GetMapping("/info")
    public ResponseEntity<UserDTO> queryUserNameAndPassword(
            @RequestParam("username")String username,
            @RequestParam("password")String password
    ){
        UserDTO userDTO = userService.queryUserByPhoneAndPassword(username,password);

        return ResponseEntity.ok(userDTO);
    }
}
